package com.qire.manhua.activity;

import android.os.Bundle;

import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.inject.Autowired;
import com.qire.common.constant.StartupConfigInfo;
import com.qire.common.constant.StartupConfigInfo.PromotionalEvent;
import com.qire.common.support.base.BaseActivity;
import com.qire.common.constant.Navigation;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.inject.ViewModelInjector;
import com.qire.common.utils.ThemeUtil;
import com.qire.manhua.R;
import com.qire.manhua.controller.HomeController;
import com.qire.manhua.databinding.ActivityHomeBinding;
import com.qire.manhua.dialog.SpecialEventDialog;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.enumeration.PageTabIndex;
import com.qire.manhua.model.viewModel.impl.HomeViewModel;

import androidx.annotation.Nullable;

@RouteMapping(path = Navigation.Page.HomePage)
public class HomeActivity extends BaseActivity<ActivityHomeBinding> {

    @Autowired
    int tabIndex = PageTabIndex.TAB_INDEX_INVALID;
    @Autowired
    int subTabIndex = PageTabIndex.TAB_INDEX_INVALID;

    @Autowired(injector = ViewModelInjector.class)
    private HomeViewModel viewModel;

    @Autowired(injector = EventControllerInjector.class)
    private HomeController controller;

    @Override
    public int layoutRid() {
        return R.layout.activity_home;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeUtil.transparencyBar(this);

        viewModel.onAddition(this);
        controller.onAddition(viewModel);

        showStartupPromotionalEvent();
    }

    @Override
    protected void onResume() {
        super.onResume();
        gotoTabIndex();
    }

    private void gotoTabIndex() {
        gotoTabIndex(tabIndex, subTabIndex);
        tabIndex = PageTabIndex.TAB_INDEX_INVALID;
        subTabIndex = PageTabIndex.TAB_INDEX_INVALID;
    }

    public void gotoTabIndex(int tabIndex, int subTabIndex) {
        // note: 处理由其他页面跳转到首页子页面的逻辑，选项卡和子选项卡依赖注入，所以跳转完毕后归零，普通跳转则不会处理。
        if(tabIndex != PageTabIndex.TAB_INDEX_INVALID) {
//            ToastHelper.showToast(String.format("选项卡号:%s ; 子选项卡号:%s", tabIndex, subTabIndex));
            viewModel.gotoTabPage(this, tabIndex, subTabIndex);
            viewModel.refreshBottomNavMenuUI(viewDataBinder().homeBottomNavMenu, tabIndex);
        }
    }

    /** 显示启动活动窗口 */
    private void showStartupPromotionalEvent() {
        if(StartupConfigInfo.INFO.allowShowPromotionalEvent()) {
            PromotionalEvent event = StartupConfigInfo.INFO.promotionalEvent();
            new SpecialEventDialog(this)
                    .setImagePath(event.imgUrl)
                    .setEventInfo(event.type, new ComicBookEntity(event.comicId,""), event.eventUrl, event.eventTitle)
                    .postShow();
        }
    }
}
